/***************************************************************************
 *    
 *      Copyright (c) 2009,2010 KaiTrade LLC (registered in Delaware)
 *                     All Rights Reserved Worldwide
 *
 * STRICTLY PROPRIETARY and CONFIDENTIAL
 *
 * WARNING:  This file is the confidential property of KaiTrade LLC For
 * use only by those with the express written permission and license from
 * KaiTrade LLC.  Unauthorized reproduction, distribution, use or disclosure 
 * of this file or any program (or document) is prohibited. 
 * 
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace K2AlgoSet
{
    public class PatternMatchBase : KaiTrade.Interfaces.PatternMatcher
    {
        // Create a logger for use in this class
        protected log4net.ILog m_Log;

        protected string m_Alias = "";
        protected string m_PublishName = "";

        protected KaiTrade.Interfaces.TSSet m_TSSet;
        protected string m_Name = "";
        protected string m_ParameterString = "";

        /// <summary>
        /// set of output values
        /// </summary>
        protected double[] m_Output;
        /// <summary>
        /// set of names for the outputs
        /// </summary>
        protected string[] m_OutputNames;

        protected KaiTrade.Interfaces.MatchCompleted m_MatchCompleted;

        protected List<KaiTrade.Interfaces.PatternMatcher> m_Children;

        public PatternMatchBase()
        {
            m_Log = log4net.LogManager.GetLogger("KaiPMatcher");
        }

        /// <summary>
        /// Get/Set the TSSet the matcher will work on
        /// </summary>
        public virtual KaiTrade.Interfaces.TSSet TSSet
        {
            get { return m_TSSet; }
            set { m_TSSet = value; }
        }

        /// <summary>
        /// Alias for the patternmatcher - used to publish results
        /// </summary>
        public virtual string Alias
        {
            get { return m_Alias; }
            set { m_Alias = value; }
        }

        /// <summary>
        /// Publish name for conditions and events ids
        /// </summary>
        public virtual string PublishName
        {
            get { return m_PublishName; }
            set { m_PublishName = value; }
        }
        /// <summary>
        /// Add a strategy name to the list the matcher supports publishes to
        /// </summary>
        /// <param name="myName"></param>
        public void AddStrategyName(string myName)
        {
            try
            {
                if (m_PublishName.Length > 0)
                {
                    m_PublishName += "," + myName;
                }
                else
                {
                    m_PublishName = myName;
                }
            }
            catch (Exception myE)
            {
                m_Log.Error("AddStrategyName", myE);
            }
        }

        /// <summary>
        /// matcher name
        /// </summary>
        public virtual string Name
        {
            get { return m_Name; }
        }

        /// <summary>
        /// delimited set/express for parameters
        /// </summary>
        public virtual string ParameterString
        {
            get { return m_ParameterString; }
            set
            {
                m_ParameterString = value;
                parseParameters();
            }
        }

        /// <summary>
        /// Parse the parameters and set the data into the pattern matcher
        /// </summary>
        protected  virtual void parseParameters()
        {
            
        }

        /// <summary>
        /// Array of outputs - specific to the matcher
        /// </summary>
        public virtual double[] Output
        {
            get { return m_Output; }
        }

        /// <summary>
        /// Array of outputs names- specific to the matcher
        /// </summary>
        public virtual string[] OutputNames
        {
            get { return m_OutputNames; }
        }

        /// <summary>
        /// Detect matches in the set associated with the matcher - this will
        /// typically set/publish conditions
        /// </summary>
        public virtual  void DetectMatches(bool isAdded)
        {
            
        }

        /// <summary>
        /// publish a result as a trigger - this will apply the parsing
        /// rules to the publisher name
        /// 
        /// </summary>
        /// <param name="myOutputName"></param>
        /// <param name="myStateName"></param>
        /// <param name="myState"></param>
        protected void SetTriggerResults(string myStateName, bool myState)
        {
            try
            {
                string[] myPublList = m_PublishName.Split(',');
                foreach (string myPub in myPublList)
                {
                    Factory.Instance().AppFacade.PublishTriggerValue(myPub, m_Alias + myStateName, myState);
                }
                
                
            }
            catch (Exception myE)
            {
                m_Log.Error("SetTriggerResults", myE);
            }
        }

        /// <summary>
        /// Child pattern matchers - these can be called as a
        /// hieracrchy 
        /// </summary>
        public virtual List<KaiTrade.Interfaces.PatternMatcher> Children
        { get { return m_Children; } }

        /// <summary>
        /// Get a list of the trigger names the pattern matcher publishes - if any
        /// </summary>
        public virtual  List<string> TriggerNames
        {
            get
            {
                List<string> myRet = new List<string>();
                
                return myRet;
            }
        }

        /// <summary>
        /// Called on Set updates
        /// </summary>
        public virtual  KaiTrade.Interfaces.MatchCompleted MatchCompleted
        {
            get { return m_MatchCompleted; }
            set { m_MatchCompleted = value; }
        }

        /// <summary>
        /// Set the patern matcher back to its initial state
        /// </summary>
        public virtual void Reset()
        {
        }

        /// <summary>
        /// Apply a simple price update - note bar data sets are handled as TS Sets
        /// </summary>
        /// <param name="pxUpdate"></param>
        public virtual void ApplyPriceUpdate(KaiTrade.Interfaces.PXUpdate pxUpdate)
        {

        }

        /// <summary>
        /// Get a setting value by name - used to get  parameters
        /// </summary>
        /// <param name="myName"></param>
        /// <returns></returns>
        public virtual string GetParameterValue(string myName)
        {
            return "";
        }

        /// <summary>
        /// Set a alg setting by name, used to set parameters used by the matcher
        /// </summary>
        /// <param name="myValue"></param>
        public virtual void SetParameterValue(string myName, string myValue)
        {
        }

        /// <summary>
        /// Get a list of avalaible parameters for the matcher
        /// </summary>
        /// <returns></returns>
        public virtual List<string> GetParameterNames()
        {
            return new List<string>();
        }

        /// <summary>
        /// Return the XML rendering of the Matcher
        /// </summary>
        /// <returns></returns>
        public virtual KAI.kaitns.PatternMatcher ToXMLDB()
        {
            KAI.kaitns.PatternMatcher myRet = new KAI.kaitns.PatternMatcher();
            try
            {
                myRet.Alias = this.Alias;
                myRet.PublishName = this.PublishName;
                myRet.Name = this.Name;
                myRet.ParameterString = this.ParameterString;

            }
            catch (Exception myE)
            {
                m_Log.Error("ToXMLDB", myE);
            }
            return myRet;
        }

        /// <summary>
        /// Render the matcher in XML
        /// </summary>
        /// <param name="myMatcher"></param>
        public virtual void FromXMLDB(KAI.kaitns.PatternMatcher myMatcher)
        {
            try
            {
                this.Alias=  myMatcher.Alias;
                this.PublishName = myMatcher.PublishName;
                m_Name = this.Name;
                this.ParameterString = myMatcher.ParameterString;
            }
            catch (Exception myE)
            {
                m_Log.Error("ToXMLDB", myE);
            }
        }


    }
}
